import ActionSheet, { ActionSheetTheme } from 'tdesign-miniprogram/action-sheet/index';
Page({
    data: {
        region: ['云南省', '昆明市', '五华区'],
        noteData: '请选择地区',
        dataDljb: '请选择代理级别',
        qydlTypeSelectlist: [],
        type: '',
        shengCode: [],
        shiCode: [],
        code: [],
        showShiqu: 'province',
        btnLoading: false,
    },

    onLoad(options) {
        this.getqydlTypeSelect();
    },

    // 获取代理级别
    getqydlTypeSelect() {
        this.request({
            url: 'common/dataSelect/qydlTypeSelect',
            loadingTip: '加载中',
        }).then(res => {
            this.setData({
                qydlTypeSelectlist: res.data,
            });
        });
    },

    handleAction() {
        ActionSheet.show({
            theme: ActionSheetTheme.List,
            selector: '#t-action-sheet',
            context: this,
            items: [this.data.qydlTypeSelectlist[0].name, this.data.qydlTypeSelectlist[1].name, this.data.qydlTypeSelectlist[2].name],
        });
    },

    handleSelected(e) {
        if (e.detail.index == 0) {
            this.setData({
                showShiqu: 'province',
                noteData:'请选择地区',
            });
        }

        if (e.detail.index == 1) {
            this.setData({
                showShiqu: 'city',
                noteData: '请选择地区',
            });
        }

        if (e.detail.index == 2) {
            this.setData({
                showShiqu: 'region',
                noteData: '请选择地区',
            });
        }

        this.setData({
            type: e.detail.index + 1,
            dataDljb: e.detail.selected,
        });
    },

    bindRegionChange(e) {
        console.log(e.detail.code);
        this.setData({
            noteData: e.detail.value.join('-'),
            shengCode: e.detail.code,
            shiCode: e.detail.code,
            code: e.detail.code, //省市区code
        });
    },

    submit() {
        if (this.data.dataDljb == '请选择代理级别')
            return wx.showToast({
                title: '请选择代理级别',
                icon: 'none',
            });

        if (this.data.noteData == '请选择地区')
            return wx.showToast({
                title: '请选择地区',
                icon: 'none',
            });

        this.setData({
            btnLoading: true,
        });
        // 判断是哪级代理
        const isThree = this.data.showShiqu;
        // 省级（一个code）
        if (isThree == 'province') {
            this.request({
                url: 'user/my/qydl/apply',
                method: 'POST',
                data: {
                    type: 1,
                    address_code: this.data.shengCode,
                },
            })
                .then(async res => {
                    console.log(res);
                    wx.showToast({
                        title: res.msg,
                    });
                    this.setData({
                        btnLoading: false,
                    });
                     await this.sleep(1000);
                    wx.navigateBack();
                })
                .catch(err => {
                    this.setData({
                        btnLoading: false,
                    });
                });
        }

        // 市级（2个code）
        if (isThree == 'city') {
            this.request({
                url: 'user/my/qydl/apply',
                method: 'POST',
                data: {
                    type: 2,
                    address_code: this.data.shiCode,
                },
            })
                .then(async res => {
                    console.log(res);
                    wx.showToast({
                        title: res.msg,
                    });
                    this.setData({
                        btnLoading: false,
                    });
                    await this.sleep(1000);
                    wx.navigateBack();
                })
                .catch(err => {
                    this.setData({
                        btnLoading: false,
                    });
                });
        }

        if (isThree == 'region') {
            this.request({
                url: 'user/my/qydl/apply',
                method: 'POST',
                data: {
                    type: 3,
                    address_code: this.data.code,
                },
            })
                .then(async res => {
                    console.log(res);
                    wx.showToast({
                        title: res.msg,
                    });
                    this.setData({
                        btnLoading: false,
                    });
                     await this.sleep(1000);
                    wx.navigateBack();
                })
                .catch(err => {
                    this.setData({
                        btnLoading: false,
                    });
                });
        }

        // this.request({
        //     url: 'user/my/qydl/apply',
        //     method: 'POST',
        //     data: {
        //         type: this.data.type,
        //         address_code: this.data.code,
        //     },
        // })
        //     .then(res => {
        //         console.log(res);
        //         wx.showToast({
        //             title: res.msg,
        //         });
        //         this.setData({
        //             btnLoading: false,
        //         });
        //     })
        //     .catch(err => {
        //         this.setData({
        //             btnLoading: false,
        //         });
        //     });
    },
});
